package com.fawvwebanma.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fawvwebanma.dao.PersonDao;
import com.fawvwebanma.dto.Result;
import com.fawvwebanma.entity.Person;
import com.fawvwebanma.entity.User;
import com.fawvwebanma.service.IPersonService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author jieye.ren
 * @date 2021/3/12
 */
@Component
@DubboService
@Slf4j
public class PersonService implements IPersonService {
    @Autowired
    PersonDao personDao;

    @Override
    public Result<List<Person>> getAllPersonsByUserId(User user) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("user_id", user.getUserId());
            map.put("status", 1);
            return Result.success(personDao.getPersonByMapStatusEq1(map));
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.error("0", "网络延迟，稍后再试......");
        }
    }

    @Override
    public Result<List<Person>> getPersonByUserIdAndName(String userId, List<String> names) {
        List<Person> persons = new ArrayList<>();
        for (String name : names) {
            Map<String, Object> map = new HashMap<>();
            map.put("user_id", userId);
            map.put("name", name);
            List<Person> personByName = personDao.getPersonByMapStatusEq1(map);
            if (personByName.size() == 0 || personByName == null) {
                return Result.error("0", "无此乘客信息，请添加......");
            } else {
                for (Person person : personByName) {
                    persons.add(person);
                } ;
            }
        }
        return Result.success(persons);
    }

    @Override
    public Result<Person> insertPerson(Person person) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", person.getUserId());
        map.put("person_id", person.getPersonId());
        List<Person> persons = personDao.getPersonByMap(map);
        person.setStatus(1);
        try {
            if (persons.size() > 0) {
                // 记录中已存在
                personDao.updatePerson(person, map);
            } else {
                // 记录中不存在
                personDao.savePerson(person);
            }
            return Result.success(person);
        } catch (Exception e) {
            return Result.error("0", "网络延迟，稍后再试......");
        }
    }

    @Override
    public Result<Person> deletePerson(Person person) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", person.getUserId());
        map.put("person_id", person.getPersonId());
        person.setStatus(0);
        personDao.updatePerson(person, map);
        return Result.success(person);
    }

    @Override
    public Result<Person> updatePerson(Person person) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", person.getUserId());
        map.put("person_id", person.getPersonId());
        map.put("status", 1);
        List<Person> persons = personDao.getPersonByMap(map);
        try {
            if (persons.size() > 0) {
                // 记录中存在
                personDao.updatePerson(person, map);
                return Result.success(person);
            } else {
                // 记录中不存在
                return Result.error("0", "您要更新的乘客不存在，请先添加乘客......");
            }
        } catch (Exception e) {
            return Result.error("0", "网络延迟，稍后再试......");
        }
    }

}
